<?php

namespace App\Models;

use App\Libraries\Common\CommonFun;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class RsReport extends Model
{
    use HasFactory;
    public $timestamps=false;
    protected $table='rs_report';
    protected $fillable=['report_name','is_show_home','create_time'];

    //查询数据
    public static function lists($page = 1, $limit = 20, $params = []){
        $ary_where = array();
        $report_name=data_get($params,'report_name',false);

        if($report_name){
            $ary_where[] = ['report_name', 'like', '%'.$report_name.'%'];
        }

        $query = self::where($ary_where);
        $count = $query->count();
        $data = $query->offset(CommonFun::getOffset($page, $limit))->limit($limit)->orderBy('id', 'desc')->get()->map(function (RsReport $rebate) use ($count) {
            $rebate = $rebate->attributesToArray();

            $rebate['is_show_home'] = $rebate['is_show_home']==0 ?'否' : '是';

            return $rebate;
        })->toArray();

        return $data ? compact('count', 'data') : false;
    }



    //查询有权的人员


    //对报表进行添加
    public static function created($data){

        if(!$data['id']){
            unset($data['id']);
            $data['create_time']=date('Y-m-d');
            $id= self::create($data)->id;
        }else{
            $model=self::where(['id'=>$data['id']])->first();
            $model->report_name=$data['report_name'];
            $model->is_show_home=$data['is_show_home'];
            $model->save();
            $id=$model->id;
        }
        //将id更新至权限表中
        RsReportAuth::where(['report_id'=>''])->orWhere(['report_id'=>null])->update(['report_id'=>$id]);
        return true;

    }
}
